﻿using System.Text.Json;
using fCloud.SensorApi.Models;
using fCloud.SensorApi.Services;
using Microsoft.AspNetCore.Mvc;
using MQTTnet;
using Newtonsoft.Json;

namespace fCloud.SensorApi.Controllers;
[ApiController]
[Route("api/[controller]/[action]")]
public class SensorController(
        IServiceScopeFactory scopeFactory
    ) : ControllerBase
{
    private readonly ITaskSchedulerService _taskSchedulerService=scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ITaskSchedulerService>();
    [HttpGet]
    public IActionResult Get()
    {
        return Ok(new { message = "Hello,Swagger" });
    }

    /// <summary>
    /// 获取Mqtt服务器上的传感器列表，用于判定设备是否在线，在线的设备更新到存储数据库，同时和redis做对比
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<IActionResult> GetMqttServerSensors()
    {
        var mqttSensorsApiResponse=await _taskSchedulerService.MergeSensorList();
        return Ok(mqttSensorsApiResponse);
    }

    [HttpGet]
    public async Task<IActionResult> GetAlarmInfos()
    {
        var alarmShowInfoList=await _taskSchedulerService.GetAlarmShowInfoList();
        return Ok(alarmShowInfoList);
    }

    [HttpPost]
    public async Task<IActionResult> ShowSensorDetails(string sensorSn)
    {
        var res = await _taskSchedulerService.GetSensorBySensorSn(sensorSn);
        return Ok(res);
    }
}